package com.itheima.ydd.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import com.itheima.ydd.service.IYddUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {

    @Autowired
    private IYddArticleService yddArticleService;
    @Autowired
    private IYddUserService yddUserService;
    @Autowired
    private IYddCateService yddCateService;

    /**
     * 文章列表 分页查询
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R page ( @RequestParam String start_time,
                    @RequestParam String end_time,
                    @RequestParam String title,
                    @RequestParam String cateid,
                    @RequestParam String page,
                    @RequestParam String limit ){
        //创建分页查询构造器
        Page<YddArticle> yddArticlePage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));
        //创建条件构造器
        log.info("分页数据:{}"+start_time,end_time,title,cateid,page,limit);
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(start_time) && StringUtils.isNotEmpty(end_time)){
            lqw.between(YddArticle::getCreateDate,start_time,end_time);
        }
        lqw.like(StringUtils.isNotEmpty(title),YddArticle::getTitle,title);
        if (StringUtils.isNotEmpty(cateid)){
            lqw.eq(YddArticle::getCateid,cateid);
        }
        lqw.orderByDesc(YddArticle::getCreateDate);
        //进行分页查询
        yddArticleService.page(yddArticlePage,lqw);
       List<YddArticle> yddArticles = yddArticlePage.getRecords().stream().map(yddArticle -> {
            YddCate yddCate = yddCateService.getById(yddArticle.getCateid());
            yddArticle.setCatename(yddCate.getCatename());
           /*Long createDate = yddArticle.getCreateDate()/1000L;
           yddArticle.setCreateDate(createDate);*/
           //1657769785522
           //1635850884
           //1635850884998
           //1657772070486
           return yddArticle;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",yddArticlePage.getTotal()); //总数
        map.put("totalPages",yddArticlePage.getPages()); //总页数
        map.put("pageSize",limit); //每页数量
        map.put("currentPage",page); //当前页码
        map.put("data",yddArticles);//查询到的数据
        R r = R.success("ydd-article", "page");
        r.setData(map);
        return r;

    }

    /**
     * 文章删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delete(String id){

        yddArticleService.removeById(id);
        return R.success("delete","删除成功");

    }


    /**
     * 添加文章
     * @param map
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Map map){

        String s = JSON.toJSONString(map);
        YddArticle yddArticle = JSON.parseObject(s, YddArticle.class);
        Long createDate = (Long) map.get("create_date");
        yddArticle.setCreateDate(createDate);

        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.eq(YddArticle::getTitle,yddArticle.getTitle());
        int count = yddArticleService.count(yddArticleLambdaQueryWrapper);
        if (count>0){
            //return new ResultData(1,"文章名重复","");
            return ResultData.error("文件名重复");
        }

        yddArticleService.save(yddArticle);
        //return new ResultData(0,"添加成功","");
        return ResultData.ok("添加成功","");

    }

    /**
     * 查询 数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        YddArticle yddArticle = yddArticleService.getById(id);
       // return R.success("roeInfo",yddArticle);
        return ResultData.ok("",yddArticle);

    }

    /**
     * 文章修改
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle yddArticle){

        //删除原本数据
        yddArticleService.removeById(yddArticle.getId());

        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.eq(YddArticle::getTitle,yddArticle.getTitle());
        int count = yddArticleService.count(yddArticleLambdaQueryWrapper);
        if (count>0){
            //return new ResultData(1,"文章名重复","");
            return ResultData.error("文件名重复");
        }
        Integer cateid = yddArticle.getCateid();
        YddCate cate = yddCateService.getById(cateid);
        yddArticle.setCatename(cate.getCatename());
        yddArticleService.save(yddArticle);
        //return new ResultData(0,"修改成功","");
        return ResultData.ok("修改成功","");

    }

}
